package edu.zju.gis.dbfg.fileserver.util.reader;

import edu.zju.gis.dbfg.common.exception.DataReaderException;
import lombok.Getter;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Hu
 * @date 2019/6/19
 * 数据读取基类
 **/
@Getter
public abstract class DataReader implements Closeable {

    protected String filename;

    protected String[] headers;

    public DataReader filename(String filename) {
        this.filename = filename;
        return this;
    }

    protected void check() {
        if (this.filename == null) throw new DataReaderException("none input data file set yet");
    }

    public void init() {
        this.check();
    }

    public abstract String next();

    public List<String> next(int size) {
        if (size < 0) return null;
        List<String> result = new ArrayList<>();
        String feature = this.next();
        while (feature != null) {
            result.add(feature);
            if (result.size() == size) break;
            feature = this.next();
        }
        return result;
    }

    public List<String> read() {
        List<String> result = new ArrayList<>();
        String feature = this.next();
        while (feature != null) {
            result.add(feature);
            feature = this.next();
        }
        return result;
    }

    protected abstract String[] readHeader();

}
